package com.cyt.reward.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.reward.dao.RewardCashOutDao;
import com.cyt.reward.dao.RewardStaffDao;
import com.cyt.reward.model.po.RewardCashOut;
import com.cyt.reward.model.po.RewardStaff;
import com.cyt.reward.model.vo.RewardCashOutVo;
import com.cyt.reward.service.RewardCashOutService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 打赏功能-提现记录Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-24 17:32:36
 */
@Service("rewardCashOutService")
public class RewardCashOutServiceImpl extends BaseServiceImpl<RewardCashOut> implements RewardCashOutService {

    @Resource
    private RewardCashOutDao rewardCashOutDao;
    @Resource
    private RewardStaffDao rewardStaffDao;

    @Autowired
    public void setBaseDao(RewardCashOutDao rewardCashOutDao) {
        this.baseDao = rewardCashOutDao;
    }

    /**
     * 分页查询
     *
     * @param rewardCashOutVo 接受参数对象
     * @param pageNum         分页数
     * @param pageSize        每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 17:32:36
     */
    @Override
    public ReturnResult page(RewardCashOutVo rewardCashOutVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<RewardCashOutVo> list = rewardCashOutDao.selectByCondition(rewardCashOutVo);
        PageInfo<RewardCashOutVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param rewardCashOutVo 参数接受对象
     * @param sysUser         当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 17:32:36
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(RewardCashOutVo rewardCashOutVo, SysUser sysUser) {
        if (StrUtil.isEmpty(rewardCashOutVo.getId())) {
            RewardCashOut rewardCashOut = new RewardCashOut(sysUser);
            rewardCashOut.setApplyId(rewardCashOutVo.getApplyId());
            rewardCashOut.setStaffId(rewardCashOutVo.getStaffId());
            rewardCashOut.setStaffName(rewardCashOutVo.getStaffName());
            rewardCashOut.setStaffJobNum(rewardCashOutVo.getStaffJobNum());
            rewardCashOut.setMoney(Double.parseDouble(rewardCashOutVo.getMoney()));
            rewardCashOut.setAccount(rewardCashOutVo.getAccount());
            int num = rewardCashOutDao.insert(rewardCashOut);
            if (num > 0) {
                updateRewardStaff(rewardCashOut.getStaffId(), rewardCashOut.getApplyId(), rewardCashOut.getMoney());
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            RewardCashOut rewardCashOut = rewardCashOutDao.selectByPrimaryKey(rewardCashOutVo.getId());
            rewardCashOut.setApplyId(rewardCashOutVo.getApplyId());
            rewardCashOut.setStaffId(rewardCashOutVo.getStaffId());
            rewardCashOut.setStaffName(rewardCashOutVo.getStaffName());
            rewardCashOut.setStaffJobNum(rewardCashOutVo.getStaffJobNum());
            rewardCashOut.setMoney(Double.parseDouble(rewardCashOutVo.getMoney()));
            rewardCashOut.setAccount(rewardCashOutVo.getAccount());
            rewardCashOut.setUpdateTime(new Date());
            rewardCashOut.setUpdateUser(sysUser.getId());
            int num = rewardCashOutDao.updateByPrimaryKey(rewardCashOut);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    private void updateRewardStaff(String staffId, String applyId, Double money) {
        Example example = new Example(RewardStaff.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", staffId);
        criteria.andEqualTo("applyId", applyId);
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        List<RewardStaff> rewardStaffList = rewardStaffDao.selectByExample(example);
        if (rewardStaffList != null && rewardStaffList.size() == 1) {
            RewardStaff rewardStaff = rewardStaffList.get(0);
            rewardStaff.setHaveMoney(rewardStaff.getHaveMoney() - money);
            rewardStaffDao.updateByPrimaryKey(rewardStaff);
        }
    }
}